package com.ruoyi.blsj.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.blsj.domain.BlsjBgsjzl;
import com.ruoyi.blsj.domain.BlsjHzxx;
import com.ruoyi.blsj.service.IBlsjBgsjzlService;
import com.ruoyi.blsj.service.IBlsjHzxxService;
import com.ruoyi.blsj.service.IMrsPatientService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.DateUtils;

import bp.da.DBAccess;
import bp.da.DataRow;
import bp.da.DataTable;

/**
 * 患者信息Controller
 * 
 * @author ruoyi
 * @date 2023-09-26
 * 
 */
@Controller
@RequestMapping("/api")
public class ApiController extends BaseController
{
    
	@Autowired
    private IMrsPatientService mrsPatientService;
	
	@Autowired
    private IBlsjBgsjzlService blsjBgsjzlService;
	
	@Autowired
    private IBlsjHzxxService blsjHzxxService;
	
	
    /**
     * 患者信息自动完成
     * 查询患者信息列表
     */
    @RequestMapping("/patient/searchKey")
    @ResponseBody
    public List<Map<String,Object>> searchKey(String key,String type)
    {
    	Map<String,Object> paramMap = new HashMap<String,Object>();
    	paramMap.put("key", key);
    	paramMap.put("type", type);
		List<Map<String,Object>> list = mrsPatientService.searchKey(paramMap);
        return list;
    }
    /**
     * 患者信息自动完成
     * @param id
     * @return
     */
    @RequestMapping("/patient/searchById")
    @ResponseBody
    public Map<String,Object> searchById(String id)
    {
		Map<String,Object> patient = mrsPatientService.searchById(id);
        return patient;
    }
    
    /**
     * 当时人信息自动完成
     * @param key
     * @return
     */
    @RequestMapping("/user/searchKey")
    @ResponseBody
    public List<Map<String,Object>> searchUserKey(String key)
    {
		List<Map<String,Object>> list = mrsPatientService.searchUserKey(key);
        return list;
    }
    /**
     * 当时人信息自动完成
     * @param id
     * @return
     */
    @RequestMapping("/user/searchById")
    @ResponseBody
    public Map<String,Object> searchUserById(String id)
    {
		Map<String,Object> user = mrsPatientService.searchUserById(id);
        return user;
    }
    
    @RequestMapping("/syncBlsjData")
    @ResponseBody
    public Map<String,Object> syncData(String WorkID,String FlowNo,String UserNo)
    {
		//通过flowNo找到流程数据主表如：ND7rpt，从表nd701dsrxx，从表nd701dtl1【把表名成功hzxx】
    	
    	//写入从表的时候，最好加上flowNO,workid，否则后面不好跟踪数据来源
    	System.out.println("WorkID==>" + WorkID + " flowNo===>"+ FlowNo+" UserNo==>" + UserNo);
//    	WorkID = "433836390";
//    	FlowNo = "001";
    	String sql = "select * from nd"+Integer.parseInt(FlowNo)+"rpt where OID="+WorkID;
    	DataTable dt = DBAccess.RunSQLReturnTable(sql);
    	System.out.print(dt.getValue(0, "SFKS"));
    	
    	BlsjBgsjzl blsjBgsjzl = new BlsjBgsjzl();
    	for (DataRow dr : dt.Rows){
    		//查询父节点类型
    		sql = "SELECT * FROM WF_Flow WHERE No='"+FlowNo+"'";
        	DataTable dt1x = DBAccess.RunSQLReturnTable(sql);
        	blsjBgsjzl.setpType(dt1x.getValue(0, "FK_FlowSort").toString());//主分类表单单号类型
        	
    		blsjBgsjzl.setWorkId(Long.parseLong(dr.getValue("OID").toString()));//申请表id
    		blsjBgsjzl.setType(FlowNo);//表单单号类型
    		blsjBgsjzl.setBgsjNo(dr.getValue("BillNo").toString());//申请单号
    		blsjBgsjzl.setSjfssj(DateUtils.parseDate(dr.getValue("SJFSRQ").toString()));//事件发生日期
    		blsjBgsjzl.setSfks(dr.getValue("SFKS").toString());//事发科室
    		if("-1".equals(dr.getValue("SJFSCS").toString())){
    			blsjBgsjzl.setSjfscs("");//事件发生场所
    		}else{
    			blsjBgsjzl.setSjfscs(dr.getValue("SJFSCS").toString());//事件发生场所
    		}
    		
    		if("-1".equals(dr.getValue("YXCD").toString())){
    			blsjBgsjzl.setSjdj("");
    		}else{
    			blsjBgsjzl.setSjdj(dr.getValue("YXCD").toString());//事件等级
    		}
    		
    		if("-1".equals(dr.getValue("SJYZCD").toString())){
    			blsjBgsjzl.setSjyzcd("");
    		}else{
    			blsjBgsjzl.setSjyzcd(dr.getValue("SJYZCD").toString());//事件严重程度
    		}
    		
    		if("-1".equals(dr.getValue("ZCFSDKN").toString())){
    			blsjBgsjzl.setZcfsjh("");
    		}else{
    			blsjBgsjzl.setZcfsjh(dr.getValue("ZCFSDKN").toString());//再次发生机会
    		}
    		
    		if("-1".equals(dr.getValue("SJFXD").toString())){
    			blsjBgsjzl.setSjfxd("");
    		}else{
    			blsjBgsjzl.setSjfxd(dr.getValue("SJFXD").toString());//事件风险度
    		}
//    		//blsjBgsjzl.setSfGlry(sfGlry);//是否关联人员
    		if("-1".equals(dr.getValue("YS").toString())){
	    		blsjBgsjzl.setSfYs("N");//是否医师
    			blsjBgsjzl.setYs("");
    		}else{
    			blsjBgsjzl.setSfYs("Y");//是否医师
    			blsjBgsjzl.setYs(dr.getValue("YS").toString());//医师
    		}
    		if("-1".equals(dr.getValue("HLRY").toString())){
	    		blsjBgsjzl.setSfHlry("N");//是否护理人员
    			blsjBgsjzl.setHlry("");
    		}else{
    			blsjBgsjzl.setSfHlry("Y");//是否护理人员
    			blsjBgsjzl.setHlry(dr.getValue("HLRY").toString());//护理人员
    		}
    		if("-1".equals(dr.getValue("YSRY").toString())){
	    		blsjBgsjzl.setSfYsry("N");//是否药事人员
    			blsjBgsjzl.setYsry("");
    		}else{
    			blsjBgsjzl.setSfYsry("Y");//是否药事人员
    			blsjBgsjzl.setYsry(dr.getValue("YSRY").toString());//药事人员
    		}
    		if("-1".equals(dr.getValue("YYKRY").toString())){
	    		blsjBgsjzl.setSfYykry("N");//是否营养科人员
    			blsjBgsjzl.setYykry("");
    		}else{
    			blsjBgsjzl.setSfYykry("Y");//是否营养科人员
    			blsjBgsjzl.setYykry(dr.getValue("YYKRY").toString());//营养科人员
    		}
    		if("-1".equals(dr.getValue("JYRY").toString())){
	    		blsjBgsjzl.setSfJyry("N");//是否检验人员
    			blsjBgsjzl.setJyry("");
    		}else{
    			blsjBgsjzl.setSfJyry("Y");//是否检验人员
    			blsjBgsjzl.setJyry(dr.getValue("JYRY").toString());//检验人员
    		}
    		if("-1".equals(dr.getValue("WLZLRY").toString())){
	    		blsjBgsjzl.setSfWlzlry("N");//是否物理治疗人员
    			blsjBgsjzl.setWlzlry("");
    		}else{
    			blsjBgsjzl.setSfWlzlry("Y");//是否物理治疗人员
    			blsjBgsjzl.setWlzlry(dr.getValue("WLZLRY").toString());//物理治疗人员
    		}
    		if("-1".equals(dr.getValue("FSJSRY").toString())){
	    		blsjBgsjzl.setSfFsjsry("N");//是否放射技术人员
    			blsjBgsjzl.setFsjsry("");
    		}else{
    			blsjBgsjzl.setSfFsjsry("Y");//是否放射技术人员
    			blsjBgsjzl.setFsjsry(dr.getValue("FSJSRY").toString());//放射技术人员
    		}
    		if("-1".equals(dr.getValue("XZRY").toString())){
	    		blsjBgsjzl.setSfXzry("N");//是否行政人员
    			blsjBgsjzl.setXzry("");
    		}else{
    			blsjBgsjzl.setSfXzry("Y");//是否行政人员
    			blsjBgsjzl.setXzry(dr.getValue("XZRY").toString());//行政人员
    		}
    		blsjBgsjzl.setXzryQt(dr.getValue("XZRYQT").toString());//行政人员其他
    		if("-1".equals(dr.getValue("YWZCRY").toString())){
	    		blsjBgsjzl.setSfYwzcry("N");//是否院外支持人员
    			blsjBgsjzl.setYwzcry("");
    		}else{
    			blsjBgsjzl.setSfYwzcry("Y");//是否院外支持人员
    			blsjBgsjzl.setYwzcry(dr.getValue("YWZCRY").toString());//院外支持人员
    		}
    		blsjBgsjzl.setYwzcryQt(dr.getValue("YWZCRYQT").toString());//院外支持人员其他
//    		
    		if("-1".equals(dr.getValue("DSRYSFSYYZG").toString())){
    			blsjBgsjzl.setSfYyzg("");
    		}else{
    			blsjBgsjzl.setSfYyzg(dr.getValue("DSRYSFSYYZG").toString());//是否医院职工
    		}
    		blsjBgsjzl.setZggh(dr.getValue("ZGGH").toString());//职工工号
    		if("-1".equals(dr.getValue("FKJS").toString())){
	    		blsjBgsjzl.setSfFkjs("N");//是否访客家属
    			blsjBgsjzl.setFkjs("");
    		}else{
    			blsjBgsjzl.setSfFkjs("Y");//是否访客家属
    			blsjBgsjzl.setFkjs(dr.getValue("FKJS").toString());//访客家属
    		}
    		if("-1".equals(dr.getValue("YG").toString())){
	    		blsjBgsjzl.setSfYg("N");//是否员工
    			blsjBgsjzl.setYg("");
    		}else{
    			blsjBgsjzl.setSfYg("Y");//是否员工
    			blsjBgsjzl.setYg(dr.getValue("YG").toString());//员工
    		}
    		if("-1".equals(dr.getValue("YQSB").toString())){
	    		blsjBgsjzl.setSfYqsb("N");//是否仪器设备
    			blsjBgsjzl.setYqsb("");
    		}else{
    			blsjBgsjzl.setSfYqsb("Y");//是否仪器设备
    			blsjBgsjzl.setYqsb(dr.getValue("YQSB").toString());//仪器设备
    		}
//    		blsjBgsjzl.setSfHz(sfHz);//是否患者
    		blsjBgsjzl.setSjfsjg(dr.getValue("SJFSJG").toString());//事件发生经过
    		blsjBgsjzl.setCreateTime(DateUtils.parseDate(dr.getValue("RDT").toString()));
    		blsjBgsjzl.setCreateBy(dr.getValue("ShenQingRen").toString());
    		blsjBgsjzl.setState("JXZ");//进行中
    		blsjBgsjzlService.insertBlsjBgsjzl(blsjBgsjzl);
    	}
		int id = Integer.parseInt(FlowNo)*100 + 1;
		
		sql = "select * from nd"+id+"dtl1 where RefPK="+WorkID;
    	dt = DBAccess.RunSQLReturnTable(sql);
		
		BlsjHzxx blsjHzxx = null;
		for (DataRow dr : dt.Rows){
			blsjHzxx = new BlsjHzxx();
			blsjHzxx.setBgsjzlId(Long.parseLong(dr.getValue("RefPK").toString()));//
			blsjHzxx.setZyh(dr.getValue("ZYH").toString());
			blsjHzxx.setMzh(dr.getValue("MZH").toString());
			blsjHzxx.setHzxm(dr.getValue("HZXM").toString());
			blsjHzxx.setHzxb(dr.getValue("XB").toString());
			blsjHzxx.setHzcsrq(dr.getValue("HZCSRQ").toString());
			blsjHzxx.setHznl(Long.parseLong(dr.getValue("HZNL").toString()));
			blsjHzxx.setHzdh(dr.getValue("HZDH").toString());
			blsjHzxx.setHztz(dr.getValue("HZTZ").toString());
			blsjHzxx.setHzmz(dr.getValue("HZMZ").toString());
			blsjHzxx.setHzzzys(dr.getValue("HZZZYS").toString());
			blsjHzxx.setHzszkb(dr.getValue("HZSZKB").toString());
			blsjHzxx.setHzjylb(dr.getValue("HZJYLB").toString());
			blsjHzxx.setHzlczd(dr.getValue("LCZD").toString());
//			blsjHzxx.setCreateTime(DateUtils.parseDate(dr.getValue("RDT").toString()));
//			blsjHzxx.setCreateBy(dr.getValue("ShenQingRen").toString());
			blsjHzxxService.insertBlsjHzxx(blsjHzxx);
		}
    	Map<String,Object> retMap = new HashMap<String,Object>();
    	retMap.put("code", 200);
    	retMap.put("msg", "同步成功");
        return retMap;
    }
    
    @RequestMapping("/UpdateBlsjDataState")
    @ResponseBody
    public Map<String,Object> UpdateBlsjDataState(String WorkID,String FlowNo,String UserNo)
    {
    	
    	BlsjBgsjzl blsjBgsjzl = new BlsjBgsjzl();
    	blsjBgsjzl.setWorkId(Long.parseLong(WorkID));
    	blsjBgsjzl.setState("WC");//完成
    	blsjBgsjzlService.updateBlsjBgsjzlByWorkId(blsjBgsjzl);
    	Map<String,Object> retMap = new HashMap<String,Object>();
    	retMap.put("code", 200);
    	retMap.put("msg", "成功");
        return retMap;
    }
    
}
